package org.ubik.filtertreetable.tree.sample;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.tree.DefaultMutableTreeNode;

import org.ubik.filtertreetable.tree.JFilterTree;

/**
 * this class is a sample of filter tree. We can run this GUI with main method  
 * @author rcollin
 *
 */
@SuppressWarnings("serial")
public class MySampleFilterTree extends JPanel{
	
	/** main menu */
	private final JPopupMenu menu = new JPopupMenu();
	
	/** add node menu */
	private JMenuItem addNode = new JMenuItem("Add Node");
	
	/** delete node menu */ 
	private JMenuItem deleteNode = new JMenuItem("Delete Node");
	
	/** rename node menu */
	private JMenuItem renameNode = new JMenuItem("Rename Node");
	
	/** filtered tree */
	private JFilterTree tree;

	/** root node of data model */
	private DefaultMutableTreeNode teamNode;
	
	/**
	 * default constructor
	 */
	public MySampleFilterTree() {
		initData();
		init();
		initAction();
	}

	/**
	 * init data model of tree
	 */
	private void initData() {
		teamNode = new DefaultMutableTreeNode("Team");
		DefaultMutableTreeNode olivierNode = new DefaultMutableTreeNode("Olivier");
		DefaultMutableTreeNode dewiNode = new DefaultMutableTreeNode("Dewi");
		DefaultMutableTreeNode remiNode = new DefaultMutableTreeNode("Remi");
		teamNode.add(olivierNode);
		teamNode.add(dewiNode);
		teamNode.add(remiNode);
	}


	/**
	 * init GUI component
	 */
	private void init() {
		this.setLayout(new BorderLayout());
		
		menu.add(addNode);
		menu.add(deleteNode);
		menu.add(renameNode);

		//define tree
		tree = new JFilterTree(true) {
			
			@Override
			public boolean match(Object o) {
				
				boolean match = false;
				if(o instanceof String){
					String s = (String) o; 
					String filter = getFilterString();
					if(s.toLowerCase().contains(filter.toLowerCase()))
						match = true;
				}
				return match;
				
			}
			
			@Override
			public DefaultMutableTreeNode initModel() {
				return teamNode;
			}
			
			@Override
			public void onRightClick(MouseEvent e) {
					super.onRightClick(e);
					DefaultMutableTreeNode selectedNode = getSelectedNode();
					if(selectedNode!=null){
						menu.show(e.getComponent(), e.getX(), e.getY());
					}
			}
		};

		//set renderer
		tree.setRenderer(new MySampleFilterTreeRenderer());
		
		this.add(tree, BorderLayout.CENTER);
	}

	/**
	 * init actions
	 */
	private void initAction() {
		addNode.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				tree.createNode(tree.getSelectedNode(), "new Child");
			}
		});
		deleteNode.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				tree.removeSelectedNode();
			}
		});
		renameNode.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				tree.updateSelectedNode("node updated");
			}
		});
	}

	/**
	 * main method
	 * @param args string array
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		MySampleFilterTree mainPanel = new MySampleFilterTree();
		frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
		frame.pack();
		frame.setVisible(true);
		
	}
	
	
}
